package com.chaoyong.higo.adapter;

import java.io.Serializable;
import java.util.List;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseAdapter;
import com.chaoyong.higo.bean.ListEntity;
import com.chaoyong.higo.cainiaowo.CartProvider;
import com.chaoyong.higo.fragment.FragAllGoods_z;
import com.chaoyong.higo.utils.TransferTool;
import com.chaoyong.higo.utils.Values;
import com.chaoyong.higo.utils.ViewHolder;
import com.chaoyong.higo.view.ConfirmDialog;
import com.chaoyong.higo.view.ConfirmDialog.ClickListenerInterface;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class FragAllGoodsAdapter extends BaseAdapter<ListEntity> {

	/** 商品图片 */
	private ImageView itemshop_image_img;
	/** 商品名称 */
	private TextView itemshops_name_tx;
	/** 商品价格 */
	private TextView itemshops_price_tx;
	/** 已参与多少人次 */
	private TextView itemshops_count_tx;
	/** 总共需要多少人次 */
	private TextView itemshops_total_tx;
	/** 还剩多少人次 */
	private TextView itemshops_left_tx;
	/** 进度条 */
	private ProgressBar test_progressbar;
	/** 添加到购物车 */
	private ImageView itemshop_shop_imgbt;
	// private String id;
	private OnCartClick click;
	private Context context;
	
	private ImageView[] ivs = new ImageView[100];
	
private ListEntity entity;
private CartProvider cartProvider;
	
	public FragAllGoodsAdapter(Context context) {
		super(context);
		this.context = context;
		cartProvider = new CartProvider(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_shops, null);
		}

		findViews(convertView);

		// Holder.price_tx.setText(bean.get(position).getGoods_price());
		// Holder.name_tx.setText(bean.get(position).getGoods_name());
		// Holder.total_tx.setText(TransferTool.getString(bean.get(position).getGoods_price(),
		// "."));
		// Holder.count_tx.setText(TransferTool.toString(count));
		// Holder.left_tx.setText(TransferTool.toString(TransferTool.Strtoint(price,".")-count));
		// Double d=(double) (count/(TransferTool.Strtoint(price, ".")/100));
		// Holder.test_progressbar.setProgress(d.intValue());
		// BitmapUtils tUtil = new BitmapUtils(context);
		// tUtil.display(Holder.imgview,Values.BASE_IMAGE_URL+bean.get(position).getFace_img().get(0));
		// 商品id

		final String uri = mList.get(position).getFace_img().get(0);
		ImageLoader.getInstance().displayImage(Values.BASE_IMAGE_URL + uri, itemshop_image_img);
		// 价格
		Double goods_price = mList.get(position).getGoods_price();
		itemshops_price_tx.setText(goods_price + "");
		// 名称
		String goods_name = mList.get(position).getGoods_name();
		itemshops_name_tx.setText("(第" + mList.get(position).getExpect() + context.getResources().getString(R.string.expect) + ")" + goods_name);
		// 总共
		String totals = TransferTool.getString(goods_price + "", ".");
		itemshops_total_tx.setText(totals);
		// 已参与
		int count_tx = mList.get(position).getSurplus();
		itemshops_count_tx.setText(count_tx + "");
		// 还剩
		int left_tx = TransferTool.Strtoint(goods_price + "", ".") - count_tx;
		itemshops_left_tx.setText(left_tx + "");
		Double d = (double) (count_tx / (TransferTool.Strtoint(goods_price + "", ".") / 100.00));
		//Double double1 = (double) (listr.get(arg1).getSurplus()) / (TransferTool.toInt(total) / 100);
		//Double d = (double) (count_tx / Double.parseDouble(totals));
		if(d < 1 && d > 0){
			test_progressbar.setProgress(1);
		}else{
			test_progressbar.setProgress(d.intValue());
		}
		ivs[position] = itemshop_image_img;
		//商品期数
//		final String expect = mList.get(position).getExpect();

		itemshop_shop_imgbt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/*ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "确定删除？", "确定", "取消");
				confirmDialog.show();
				confirmDialog.setClicklistener(new ClickListenerInterface() {
					
					@Override
					public void doConfirm() {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void doCancel() {
						// TODO Auto-generated method stub
						
					}
				});*/
				cartProvider.put(mList.get(position));
				// Toast.makeText(mContext, "已加入到购物车",
				// Toast.LENGTH_SHORT).show();
				click.onCartClick(mList.get(position).getId(),mList.get(position).getExpect(),mList.get(position));
//				entity = mList.get(position);
				//FragAllGoods_z.onAnimImage(uri);
			}
		});
		return convertView;
	}

	/*protected void onAnimImage(ImageView iv) {
		// TODO Auto-generated method stub
		//初始化  
		Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
		//设置动画时间                
		translateAnimation.setDuration(2000);  
		iv.startAnimation(translateAnimation); 
	}*/

	private void findViews(View convertView) {
		itemshop_image_img = ViewHolder.get(convertView, R.id.itemshop_image_img);
		itemshops_name_tx = ViewHolder.get(convertView, R.id.itemshops_name_tx);
		itemshops_price_tx = ViewHolder.get(convertView, R.id.itemshops_price_tx);
		itemshops_count_tx = ViewHolder.get(convertView, R.id.itemshops_count_tx);
		itemshops_total_tx = ViewHolder.get(convertView, R.id.itemshops_total_tx);
		itemshops_left_tx = ViewHolder.get(convertView, R.id.itemshops_left_tx);
		test_progressbar = ViewHolder.get(convertView, R.id.test_progressbar);
		itemshop_shop_imgbt = ViewHolder.get(convertView, R.id.itemshop_shop_imgbt);
	}

	public void setOnCartClickListener(OnCartClick click) {
		this.click = click;
	}

	public interface OnCartClick {
		public void onCartClick(String id,String expect,Serializable sb);
	}


}
